const state = {
  cart: JSON.parse(localStorage.cart || '[]')
}

const mutations = {
  'ADD_COUNT'(state, payload) {
    let flag = false
    state.cart.forEach(item => {
      if (item.id === payload.id) {
        item.count += payload.count
        flag = true
      }
    })
    if (!flag) {
      state.cart.push(payload)
    }
    localStorage.cart = JSON.stringify(state.cart)
    // console.log(state, payload)
  }
}
const actions = {}
const getters = {
  getAllCount(state) {
    let c = 0
    state.cart.forEach(item => {
      c += item.count
    })
    // console.log(state.cart)
    return c
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}